|
|
|
|
|
|
|
Figure 28.16
The results of a simple query were created with the Execute method of the ADODB. Connection object. |
|
|
|
|
|
|
|
|
The Execute method of the Connection object returns a Recordset object, which is stored in the variable rs. Next, a While loop is used to move through the recordset, adding each author's name and birth year to the list box. |
|
|
|
|
|
|
|
|
You can also use the Execute method to run SQL statements that do not return a recordset against the database, as in the following examples: |
|
|
|
|
|
|
|
|
cn.Execute "Delete from Authors where Author = 'Jones, Amanda'"
cn.Execute _
"INSERT INTO AUTHORS ([Author],[Year Born]) Select 'Siler, Brian',1972" |
|
|
|
|
|
|
|
|
The first statement deletes a record from the Authors table, and the second statement inserts a new record. (Remember, these examples were written for Access; in another database, the SQL syntax might be slightly different.) |
|
|
|
|
|
|
|
|
So far in this chapter, you have used a simple connection string that specifies the BIBLIO data source name, which was set up previously in the ODBC Data Source Manager. If you do not want to depend on a DSN's being set up, you can establish a DSN-less connection by giving ADO more information in the ConnectionString property, as in the following examples for a Microsoft Access database: |
|
|
|
|
|
|
|
|
cn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.3.51;Data Source=d:\vb6\biblio.mdb"
cn.ConnectionString = _
"Driver=Microsoft Access Driver (*.mdb);DBQ=D:\VB6\biblio.mdb" |
|
|
|
|
|
|
|
|
|
Even if you use a DSN-less connection, the appropriate drivers and support files still must be installed on the user's PC.  |
|
|
|
|
|
|
|
|
|
|
The preceding connection string names the driver rather than the DSN. For the Access driver, the DBQ parameter points to the database name. You can also specify other OLE DB providers, such as the following string used to connect to a Microsoft SQL Server: |
|
|
|
|
|
|
|
|
"Provider=SQLOLEDB.1;Password=groovy:User ID=apowers:Location=SQLSRV1; _
database=employee" |
|
|
|
|
|